// 计算心率和血压判定分
export const calculateScore = (value: number, baseline: number): number => {
    if (baseline - 10 <= value && value <= baseline + 10) return 100;
    if ((baseline - 20 <= value && value < baseline - 10) || (baseline + 10 < value && value <= baseline + 20)) return 80;
    if ((baseline - 30 <= value && value < baseline - 20) || (baseline + 20 < value && value <= baseline + 30)) return 60;
    if (value < baseline - 30 || baseline + 30 < value) return 40;
    return 0;
};

// 血压判定分
export const calculateBloodPressureScore = (value: number, baseline: number): number => {
    if (baseline - 10 <= value && value <= baseline + 10) return 100;
    if ((baseline - 20 <= value && value < baseline - 10) || (baseline + 10 < value && value <= baseline + 20)) return 80;
    if ((baseline - 30 <= value && value < baseline - 20) || (baseline + 20 < value && value <= baseline + 30)) return 40;
    if (value < baseline - 30 || baseline + 30 < value) return 0;
    return 0;
};

// 血氧饱和度判定分
export const calculateSpo2Score = (value: number): number => {
    if (95 <= value && value <= 100) return 100;
    if (90 <= value && value < 95) return 80;
    if (85 <= value && value < 90) return 60;
    if (value < 85) return 0;
    return 0;
};

// 压力指数判定分
export const calculateStressIndexScore = (value: number): number => {
    if (value <= 2.5) return 100;
    if (2.5 <= value && value < 3) return 80;
    if (3 <= value && value < 4) return 60;
    if (4 <= value) return 40;
    return 0;
};

// 心康风险判定分
export const calculateHeartAttackRiskScore = (value1: number): number => {
    if (value1 <= 0.1) return 100;
    return 0;
};

// 反应时判定分
export const calculateReactionTimeScore = (value: number | undefined, baseline: number): number => {
    if (value === undefined) return 0;
    if (value <= baseline) return 100;
    if (baseline < value && value <= baseline + 50) return 90;
    if (baseline + 50 < value && value <= baseline + 100) return 80;
    if (baseline + 100 < value && value <= baseline + 200) return 60;
    if (baseline + 200 < value) return 40;
    return 0;
};

// 握力判定分
export const calculateGripStrengthScore = (value: number | undefined, baseline: number): number => {
    if (value === undefined) return 0;
    if (baseline <= value) return 100;
    if (baseline - 5 <= value && value < baseline) return 80;
    if (baseline - 10 <= value && value < baseline - 5) return 60;
    if (baseline - 15 <= value && value < baseline - 10) return 40;
    if (value < baseline - 15) return 0;
    return 0;
};

// 临界闪光融合频率判定分
export const calculateFlickerScore = (value: number | undefined): number => {
    if (value === undefined) return 0;
    if (value === 1) return 100; // 结果全对
    if (value === 0) return 0;   // 结果至少有一个错
    return 0;
};

// 肺活量判定分
export const calculateVitalCapacityScore = (value: number | undefined, baseline: number): number => {
    if (value === undefined) return 0;
    if (baseline <= value) return 100;
    if (baseline - 300 <= value && value < baseline) return 80;
    if (baseline - 600 <= value && value < baseline - 300) return 60;
    if (baseline - 900 <= value && value < baseline - 600) return 40;
    if (value < baseline - 900) return 0;
    return 0;
};

// 呼气酒精含量判定分
export const calculateAlcoholScore = (value: number | undefined): number => {
    if (value === undefined || value === -1) return 0;
    if (value <= 20) return 100;
    if (value > 20) return 0;
    return 0;
};

// 计算健康指数
export const calculateHealthIndex = (
    heartRateScore: number,
    bloodPressureScore: number,
    spo2Score: number,
    stressIndexScore: number,
    heartAttackRiskScore: number
): number => {
    // 如果心康风险判定分=100，那么健康指数=(心率判定分+血压判定分+血氧饱和度判定分+压力指数判定分)/4
    // 否则健康指数=0
    if (heartAttackRiskScore === 100) {
        return Number(((heartRateScore + bloodPressureScore + spo2Score + stressIndexScore) / 4).toFixed(2));
    }
    return 0;
};

// 计算疲劳指数
export const calculateFatigueIndex = (
    flickerScore: number,
    alcoholScore: number,
    reactionTimeScore: number,
    gripStrengthScore: number,
    vitalCapacityScore: number
): number => {
    // 如果临界闪光融合频率判定分=100且呼气酒精含量判定分=100，
    // 那么疲劳指数=(反应时判定分+握力判定分+肺活量判定分)/3；
    // 否则，疲劳指数=0
    if (flickerScore === 100 && alcoholScore === 100) {
        return Number(((reactionTimeScore + gripStrengthScore + vitalCapacityScore) / 3).toFixed(2));
    }
    return 0;
};

// 计算综合安全健康指数
export const calculateOverallIndex = (
    healthIndex: number,
    emotionIndex: number,
    fatigueIndex: number
): number => {
    return Number(((healthIndex + emotionIndex + fatigueIndex) / 3).toFixed(2));
};

export const calculateLevel = (score: number): number => {
    return score >= 85 ? 1 : score >= 68 ? 2 : 3
}